import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import {nanoid} from 'nanoid'
Vue.use(Vuex)

const actions = {
	addSaying(miniStore){
		axios.get('http://api.uixsj.cn/hitokoto/get?type=social').then(
			response => {
				miniStore.commit('ADD_SAYING',response.data)
			},
			error => {
				alert(error)
			}
		)
	}
}

const mutations = {
	INCREMENT_SCHOOL_AGE(state,value){
		state.school.age += value
	},
	ADD_SAYING(state,value){
		const sayingObj = {id:nanoid(),title:value}
		state.sayingList.unshift(sayingObj)
	}
}

const state = {
	school:{
		name:'尚硅谷',
		address:'宏福科技园',
		tel:'010-56253825',
		age:8
	},
	sayingList:[
		{id:'4356r7t8y6',title:'下山猛虎战群狼，败者为寇胜者为王'}
	]
}

const getters = {}

export default new Vuex.Store({
	actions,
	mutations,
	state,
	getters
})